package com.dianping.cat.consumer.state.model.transform;

import com.dianping.cat.consumer.state.model.IEntity;
import com.dianping.cat.consumer.state.model.IVisitor;
import com.dianping.cat.consumer.state.model.entity.Detail;
import com.dianping.cat.consumer.state.model.entity.Machine;
import com.dianping.cat.consumer.state.model.entity.Message;
import com.dianping.cat.consumer.state.model.entity.ProcessDomain;
import com.dianping.cat.consumer.state.model.entity.StateReport;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
































public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

/*  56 */   public DefaultXmlBuilder() { this(false); }



/*  60 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  64 */     this.m_compact = compact;
/*  65 */     this.m_sb = sb;
/*  66 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  70 */     entity.accept(this.m_visitor);
/*  71 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  75 */     this.m_level--;

/*  77 */     indent();
    this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  82 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  86 */     if (value == null) {
/*  87 */       return null;
    }

/*  90 */     String str = toString(value);
/*  91 */     int len = str.length();
/*  92 */     StringBuilder sb = new StringBuilder(len + 16);

/*  94 */     for (int i = 0; i < len; i++) {
/*  95 */       char ch = str.charAt(i);

/*  97 */       switch (ch) {
        case '<':
/*  99 */           sb.append("&lt;");
          break;
        case '>':
/* 102 */           sb.append("&gt;");
          break;
        case '&':
/* 105 */           sb.append("&amp;");
          break;
        case '"':
/* 108 */           if (!text) {
/* 109 */             sb.append("&quot;");
            break;
          }
        default:
/* 113 */           sb.append(ch);
          break;
      }

    }
/* 118 */     return sb.toString();
  }

  protected void indent() {
/* 122 */     if (!this.m_compact) {
/* 123 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 124 */         this.m_sb.append("   ");
      }
    }
  }


/* 130 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 134 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 138 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 142 */     indent();

/* 144 */     this.m_sb.append('<').append(name);

/* 146 */     int len = nameValues.length;

/* 148 */     for (int i = 0; i + 1 < len; i += 2) {
/* 149 */       Object attrName = nameValues[i];
/* 150 */       Object attrValue = nameValues[i + 1];

/* 152 */       if (attrValue != null) {
/* 153 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 157 */     if (dynamicAttributes != null) {
/* 158 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 159 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 163 */     if (text != null && closed) {
/* 164 */       this.m_sb.append('>');
/* 165 */       this.m_sb.append(escape(text, true));
/* 166 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 168 */       if (closed) {
/* 169 */         this.m_sb.append('/');
      } else {
/* 171 */         this.m_level++;
      }

/* 174 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 180 */     if (value instanceof String)
/* 181 */       return (String)value; 
/* 182 */     if (value instanceof Collection) {
/* 183 */       Collection<Object> list = (Collection)value;
/* 184 */       StringBuilder sb = new StringBuilder(32);
/* 185 */       boolean first = true;

/* 187 */       for (Object item : list) {
/* 188 */         if (first) {
/* 189 */           first = false;
        } else {
/* 191 */           sb.append(',');
        }

/* 194 */         if (item != null) {
/* 195 */           sb.append(item);
        }
      }

/* 199 */       return sb.toString();
/* 200 */     }  if (value.getClass().isArray()) {
/* 201 */       int len = Array.getLength(value);
/* 202 */       StringBuilder sb = new StringBuilder(32);
/* 203 */       boolean first = true;

/* 205 */       for (int i = 0; i < len; i++) {
/* 206 */         Object item = Array.get(value, i);

/* 208 */         if (first) {
/* 209 */           first = false;
        } else {
/* 211 */           sb.append(',');
        }

/* 214 */         if (item != null) {
/* 215 */           sb.append(item);
        }
      }

/* 219 */       return sb.toString();
    }

/* 222 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 226 */     if (text == null) {
      return;
    }

/* 230 */     indent();

/* 232 */     this.m_sb.append('<').append(name);

/* 234 */     int len = nameValues.length;

/* 236 */     for (int i = 0; i + 1 < len; i += 2) {
/* 237 */       Object attrName = nameValues[i];
/* 238 */       Object attrValue = nameValues[i + 1];

/* 240 */       if (attrValue != null) {
/* 241 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 245 */     this.m_sb.append(">");
/* 246 */     this.m_sb.append(escape(text, true));
/* 247 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 251 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 255 */     indent();

/* 257 */     this.m_sb.append('<').append(name).append(">");

/* 259 */     if (escape) {
/* 260 */       this.m_sb.append(escape(text, true));
    } else {
/* 262 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 265 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected String toString(Date date, String format) {
/* 269 */     if (date != null) {
/* 270 */       return (new SimpleDateFormat(format)).format(date);
    }
/* 272 */     return null;
  }


  protected String toString(Number number, String format) {
/* 277 */     if (number != null) {
/* 278 */       return (new DecimalFormat(format)).format(number);
    }
/* 280 */     return null;
  }




/* 286 */   public void visitDetail(Detail detail) { startTag("detail", true, null, new Object[] { "id", detail.getId(), "total", Long.valueOf(detail.getTotal()), "size", toString(Double.valueOf(detail.getSize()), "0.0"), "totalLoss", Long.valueOf(detail.getTotalLoss()) }); }



  public void visitMachine(Machine machine) {
/* 291 */     startTag("machine", null, new Object[] { "ip", machine.getIp(), "total", Long.valueOf(machine.getTotal()), "totalLoss", Long.valueOf(machine.getTotalLoss()), "maxTps", toString(Double.valueOf(machine.getMaxTps()), "0.0"), "avgTps", toString(Double.valueOf(machine.getAvgTps()), "0.0"), "blockTotal", Long.valueOf(machine.getBlockTotal()), "blockLoss", Long.valueOf(machine.getBlockLoss()), "blockTime", Long.valueOf(machine.getBlockTime()), "pigeonTimeError", Long.valueOf(machine.getPigeonTimeError()), "networkTimeError", Long.valueOf(machine.getNetworkTimeError()), "dump", Long.valueOf(machine.getDump()), "dumpLoss", Long.valueOf(machine.getDumpLoss()), "size", toString(Double.valueOf(machine.getSize()), "0.0"), "delaySum", toString(Double.valueOf(machine.getDelaySum()), "0.0"), "delayAvg", toString(Double.valueOf(machine.getDelayAvg()), "0.0"), "delayCount", Integer.valueOf(machine.getDelayCount()) });

/* 293 */     if (!machine.getProcessDomains().isEmpty()) {
/* 294 */       startTag("processDomains");

/* 296 */       for (ProcessDomain processDomain : machine.getProcessDomains().values()) {
/* 297 */         processDomain.accept(this.m_visitor);
      }

/* 300 */       endTag("processDomains");
    }

/* 303 */     if (!machine.getMessages().isEmpty()) {
/* 304 */       for (Message message : machine.getMessages().values()) {
/* 305 */         message.accept(this.m_visitor);
      }
    }

/* 309 */     endTag("machine");
  }



/* 314 */   public void visitMessage(Message message) { startTag("message", true, null, new Object[] { "id", message.getId(), "time", toString(message.getTime(), "yyyy-MM-dd HH:mm:ss"), "total", Long.valueOf(message.getTotal()), "totalLoss", Long.valueOf(message.getTotalLoss()), "dump", Long.valueOf(message.getDump()), "dumpLoss", Long.valueOf(message.getDumpLoss()), "size", toString(Double.valueOf(message.getSize()), "0.0"), "delaySum", toString(Double.valueOf(message.getDelaySum()), "0.0"), "delayCount", Integer.valueOf(message.getDelayCount()), "pigeonTimeError", Long.valueOf(message.getPigeonTimeError()), "networkTimeError", Long.valueOf(message.getNetworkTimeError()), "blockTotal", Long.valueOf(message.getBlockTotal()), "blockLoss", Long.valueOf(message.getBlockLoss()), "blockTime", Long.valueOf(message.getBlockTime()) }); }



  public void visitProcessDomain(ProcessDomain processDomain) {
/* 319 */     startTag("processDomain", null, new Object[] { "name", processDomain.getName(), "total", Long.valueOf(processDomain.getTotal()), "totalLoss", Long.valueOf(processDomain.getTotalLoss()), "size", toString(Double.valueOf(processDomain.getSize()), "0.0"), "avg", toString(Double.valueOf(processDomain.getAvg()), "0.0") });

/* 321 */     if (!processDomain.getIps().isEmpty()) {
/* 322 */       for (String ip : processDomain.getIps()) {
/* 323 */         tagWithText("ip", ip, new Object[0]);
      }
    }

/* 327 */     if (!processDomain.getDetails().isEmpty()) {
/* 328 */       for (Detail detail : processDomain.getDetails().values()) {
/* 329 */         detail.accept(this.m_visitor);
      }
    }

/* 333 */     endTag("processDomain");
  }


  public void visitStateReport(StateReport stateReport) {
/* 338 */     startTag("state-report", null, new Object[] { "domain", stateReport.getDomain(), "startTime", toString(stateReport.getStartTime(), "yyyy-MM-dd HH:mm:ss"), "endTime", toString(stateReport.getEndTime(), "yyyy-MM-dd HH:mm:ss") });

/* 340 */     if (!stateReport.getMachines().isEmpty()) {
/* 341 */       for (Machine machine : stateReport.getMachines().values()) {
/* 342 */         machine.accept(this.m_visitor);
      }
    }

/* 346 */     endTag("state-report");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/state/model/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */